package cn.ranko.shiro.service;

import cn.ranko.bean.Datagrid;
import cn.ranko.bean.Pagination;
import cn.ranko.shiro.mapper.SysRoleMapper;
import cn.ranko.shiro.mapper.SysUserMapper;
import cn.ranko.shiro.mapper.SysUserRoleMapper;
import cn.ranko.shiro.model.SysPicklistvalue;
import cn.ranko.shiro.model.SysUser;
import cn.ranko.shiro.model.SysUserRole;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by user on 2017/8/29.
 */
@Service
public class SysUserService implements UserService{

    @Autowired
    SysUserMapper sysUserMapper;

    @Autowired
    SysRoleMapper sysRoleMapper;

    @Autowired
    SysUserRoleMapper sysUserRoleMapper;

    public Datagrid loadList(Pagination pagination) {
        Integer total = sysUserMapper.countOfSysUser();
        List<SysUser> rows = sysUserMapper.querySysUser(pagination);
        for(int i=0;i<rows.size();i++){
            rows.get(i).setRolenamestr(sysRoleMapper.getRolenamestr(rows.get(i).getUserId()));
        }
        Datagrid datagrid = new Datagrid();
        datagrid.setTotal(total);
        datagrid.setRows(rows);
        return datagrid;
    }

    public SysUser selectForLogin(SysUser userLogin) {
        //根据用户名和密码查询用户，是否存在
        SysUser sysUser = sysUserMapper.selectForLogin(userLogin);
        if(sysUser!=null){
            if(sysUser.getLoginpass().equals(userLogin.getLoginpass())){
                //用户名，密码正确的情况下，获取对应的角色名称，权限列表
                List<String> rolenames = sysRoleMapper.selectRolenamesByUserId(sysUser.getUserId());
                sysUser.setRolenames(rolenames);
                //设置角色对应的权限
                List<String> permissions = sysRoleMapper.selectPermissionByUserId(sysUser.getUserId());
                sysUser.setPermissions(permissions);

                return sysUser;
            }
        }
        return null;
    }

    /**
     * 查找拥有对应角色的用户
     *
     * @param roleId
     * @return
     */
    public List<SysUser> selectRoleUser(Integer roleId) {
        return sysUserMapper.selectRoleUser(roleId);
    }

    public SysUser selectByUserId(Integer user_id) {
        return sysUserMapper.selectByPrimaryKey(user_id);
    }

    public Integer delete(Integer userId) {
        SysUser user = new SysUser();
        user.setUserId(userId);
        user.setDeleted(1);
        return sysUserMapper.updateByPrimaryKeySelective(user);
    }

    public int save(SysUser user, String roleIds) {
        Integer affect = 0;

        if(user.getUserId()==0){
            affect += sysUserMapper.insertSelective(user);
        }else{
            //编辑
            sysUserMapper.updateByPrimaryKeySelective(user);
        }

        sysUserRoleMapper.deleteByUserId(user.getUserId());

        //保存权限
        List<SysUserRole> sysUserRoles = new ArrayList<>();
        String[] splits = roleIds.split(",");
        for(int i=0;i<splits.length;i++){
            SysUserRole sysUserRole = new SysUserRole();
            sysUserRole.setRoleId(Integer.parseInt(splits[i]));
            sysUserRole.setUserId(user.getUserId());
            affect += sysUserRoleMapper.insert(sysUserRole);
        }

        return affect;
    }

    public Integer updatePassword(SysUser sysUser, String password) {
        sysUser.setLoginpass(password);
        return sysUserMapper.updateByPrimaryKeySelective(sysUser);
    }
}
